package com.yuanjin.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanjin.R;
import com.yuanjin.util.LoaddataThread;

public class RegisterPagesave extends Activity {
	// 请求服务端的url
	private static String PATH = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=write";
	private TextView tvpassword;
	private TextView tvusername;
	private TextView tvconfirmpassword;
	private Button regiserbutton;
	private static URL url;
	static {
		try {
			url = new URL(PATH);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				Toast.makeText(RegisterPagesave.this, "注册成功", Toast.LENGTH_SHORT)
						.show();
				break;
			case 2:
				Toast.makeText(RegisterPagesave.this, "该用户名已被注册", Toast.LENGTH_SHORT)
						.show();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register2);
		tvpassword = (TextView) findViewById(R.id.register_password);
		tvconfirmpassword = (TextView) findViewById(R.id.register_confirmpassword);
		tvusername = (TextView) findViewById(R.id.register_username);
	}

	public void onRegisterClick(View viewO) {
		// 上传服务器的账号、密码等信息
		Map<String, String> map = new HashMap<String, String>();
		map.put("password", tvpassword.getText().toString());
		map.put("username", tvusername.getText().toString());
		// 上传服务器的调用方法地址
		String path = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=write";
		// 加入一个判断，确保密码没有输入错误
		if (tvpassword.getText().toString()
				.equals(tvconfirmpassword.getText().toString())) {
			// 开启上传服务器的线程
			LoaddataThread loaddatathread = new LoaddataThread(map, handler,
					path);
			loaddatathread.start();
		} else {
			Toast.makeText(RegisterPagesave.this, "您的密码不一致，请重新输入",
					Toast.LENGTH_SHORT).show();
		}

	}

}
